<?php
declare (strict_types=1);

namespace app\admin\controller\base;

use app\admin\service\base\OfficialAccountService;
use app\BaseController;
use think\facade\Request;
use think\response\Json;
use upload\WechatUpload;
use wechat\WsOfficialAccount;

/**
 * 公众号配置
 */
class OfficialAccount extends BaseController
{
    /**
     * 获取公众号菜单配置（数据库）
     * @param OfficialAccountService $accountService
     * @return Json
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getWxMenuList(OfficialAccountService $accountService): json
    {
        return $accountService->getWxMenuList();
    }

    /**
     * 创建公众号菜单（数据库）
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function createWxMenu(OfficialAccountService $accountService): Json
    {
        $params = Request::param();
        return $accountService->createWxMenu($params);
    }

    /**
     * 更新微信公众号菜单
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function updateWxMenu(OfficialAccountService $accountService): Json
    {
        $params = Request::param();
        return $accountService->updateWxMenu($params);
    }

    /**
     * 删除微信公众号菜单
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function deleteWxMenu(OfficialAccountService $accountService): Json
    {
        $params = [];
        //菜单ID
        $params['id'] = Request::param('id/d');
        return $accountService->deleteWxMenu($params);
    }


    /**
     * 获取当前菜单设置
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function getMenu(OfficialAccountService $accountService): Json
    {
        return $accountService->getMenuCurrent();
    }

    /**
     * 公众号验证token(测试号)
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function token(OfficialAccountService $accountService): Json
    {
        return $accountService->token();
    }

    /**
     * 查询已设置菜单列表
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function getMenuList(OfficialAccountService $accountService): Json
    {
        return $accountService->getMenuList();
    }

    /**
     * 删除所有菜单
     * @param OfficialAccountService $accountService
     * @return Json
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function deleteMenuAll(OfficialAccountService $accountService): Json
    {
        return $accountService->deleteMenuAll();
    }

    /**
     * 获取当前自动回复设置
     * @return Json
     */
    public function autoRely(OfficialAccountService $accountService): Json
    {
        return $accountService->autoRely();
    }

    /**
     * 获取关键字回复设置列表
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function getKeywordList(OfficialAccountService $accountService): Json
    {
        $where = [];
        $params = Request::param();
        if (isset($params['key']) && $params['key']) $where[] = ['key', 'like', '%' . $params['key'] . '%'];
        if (isset($params['name']) && $params['name']) $where[] = ['key', 'like', '%' . $params['name'] . '%'];
        if (isset($params['type']) && $params['type']) $where[] = ['type', '=', $params['type']];
        return $accountService->getKeywordList($where);
    }

    /**
     * 创建关键字回复
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function createKeyword(OfficialAccountService $accountService): Json
    {
        $params = Request::param();
        return $accountService->createKeyword($params);
    }

    /**
     * 更新关键字回复设置
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function updateKeyword(OfficialAccountService $accountService): Json
    {
        $params = Request::put();
        return $accountService->updateKeyword($params);
    }

    /**
     * 删除关键字回复设置
     * @param OfficialAccountService $accountService
     * @return Json
     */
    public function deleteKeyword(OfficialAccountService $accountService): Json
    {
        $params = Request::param();
        return $accountService->deleteKeyword($params);
    }

    /**
     * 素材图片/音频/视频上传
     * @return Json
     */
    public function upload(): Json
    {
        $folder = 'official_account';
        $file = request()->file('picture');
        if (!$file) {
            return jsonReturn(0, []);
        }
        //处理上传图片信息
        $image = WechatUpload::uploadFile($file, $folder, 0, 0, 1000);
        $wx = new WsOfficialAccount();
        if ($image['suffix'] == 'mp3') {
            //音频
            $res = $wx->uploadVoice($image['path']);
        } elseif ($image['suffix'] == 'mp4') {
            //视频
            $res = $wx->uploadVideo($image['path'], '无塑地球，美丽生活', '无塑地球，美丽生活');
        } else {
            //图片
            $res = $wx->uploadImage($image['path']);
        }
        unlink($image['path']);
        if (isset($res['media_id'])) {
            return jsonReturn(1, $res);
        }
        return jsonReturn(0, $res);
    }

    /**
     * 发布微信公众号菜单
     * @param OfficialAccountService $accountService
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pushMenu(OfficialAccountService $accountService): Json
    {
        return $accountService->pushWxMenu();
    }
}
